ajax({
    url:"/sa/details.json",
    success:function(res){
        res=JSON.parse(res);
        console.log(res);
        danjuan.innerHTML=render(res.data.hai)
    }
})

var danjuan=document.querySelector(".danjuan");
function render(data) {
    return data
      .map(function (item) {
        return `<div class="dang">
          <div class="sheng">
              <img src="${item.ims}" alt="" class="img" index="${item.id}">
          </div>
          <div class="xia">
            <img src="${item.ima}" alt="" class="img1"  index="${item.id}">
          </div>
          <div class="kuan">
              <h4 class="guo">${item.name}</h4>
              <span class="jia">￥${
                item.ke ? item.prices + "/" + item.ke + "克" : item.prices
              }</span>
          </div>
      </div>`;
      })
      .join("");
  }

  danjuan.onclick=function(e){
    var e=e||window.event;
    if(e.target.classList.contains("img1")){
      var id=e.target.getAttribute("index");
      location.href="../html/detailes.html?id="+id;
    }
  }

// 回到顶部
var dingbu = document.querySelector(".dingbu");
window.onscroll = function () {
  var height=window.innerHeight+scrollY;
  if(height>window.innerHeight){
    dingbu.style.display = "block";
    dingbu.onclick = function () {
      scrollTo({ top: 0, behavior: "smooth" });
    };
  }
    else{
      dingbu.style.display = "none";
    }
  };


  jQuery.validator.addMethod(
    "userTest",
    function (value) {
      let reg = /^[\u4e00-\u9fa5a-z]+$/i;
      return reg.test(value);
    },
    "用户名由汉字或者字母组成"
  );
  //验证密码
  jQuery.validator.addMethod(
    "passTest",
    function (value) {
      let reg =
        /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?![,\.;:\?]+$)[0-9a-zA-Z,,\.;:\?]{4,14}$/;
      return reg.test(value);
    },
    "密码格式错误"
  );
  //验证验证码
  jQuery.validator.addMethod(
    "yanzheng",
    function () {
      return cap.validate($("#test").val());
    },
    "验证码输入错误"
  );
  $("#form").validate({
    rules: {
      registUser: {
        required: true,
        userTest: true,
      },
      registPass: {
        required: true,
        passTest: true,
      },
      test: {
        required: true,
        yanzheng: true,
      },
    },
  
    messages: {
      registUser: {
        required: "用户名是必填的",
      },
      registPass: {
        required: "密码是必填的",
      },
      test: {
        required: "验证码是必填的",
      },
    },
    submitHandler: function (form) {
        let p=pAjax({
          url: "/sa/regist.php",
          type:'get',
          data:{
              registUser:registUser.value,
              registPass:registPass.value,
              },
          });
          p.then((res)=>{
              res=JSON.parse(res);
              console.log(res);
              if(res.code){
                  setCookie('login',registUser.value);
                  let url=localStorage.getItem('url');
                  localStorage.removeItem('url');
                  location.href=url?url:'../html/index.html'
              }
          })
  }
  })